using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ProtoGame.Input;
using ProtoGame.GameLib.Entities;
using ProtoGame.GameLib;
using ProtoGame.GameLib.GameComponents;


namespace ProtoGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class PGLogoScreen : Microsoft.Xna.Framework.DrawableGameComponent
    {
        enum ScreenState
        {
            Starting,
            Active,
            Ending,
            Inactive
        }

        SpriteBatch spriteBatch;
        BasicSprite logo;
        String logoPath;

        TimeSpan timer;
        ScreenState state;

        public PGLogoScreen(String logoPath, Game game)
            : base(game)
        {
            this.logoPath = logoPath;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            timer = TimeSpan.FromSeconds(0.5);
            state = ScreenState.Starting;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            Texture2D logoTexture = Game.Content.Load<Texture2D>(logoPath);

            logo = new BasicSprite(logoTexture);

            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            IBasicInput entrada = Game.Services.GetService(typeof(IBasicInput)) as IBasicInput;

            if(entrada.Confirm())
                timer -= timer;
            else
                timer -= gameTime.ElapsedGameTime;

            setState();
            setFade();

            logo.Update(gameTime);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Texture, SaveStateMode.SaveState);
            
                logo.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }

        private void setState()
        {
            if (timer.TotalSeconds <= 0)
            {
                switch (state)
                {
                    case ScreenState.Starting:
                        timer = TimeSpan.FromSeconds(5);
                        state = ScreenState.Active;
                        break;

                    case ScreenState.Active:
                        timer = TimeSpan.FromSeconds(0.5);
                        state = ScreenState.Ending;
                        break;

                    case ScreenState.Ending:
                        state = ScreenState.Inactive;
                        base.Enabled = false;
                        break;

                    default: break;
                }
            }
        }

        private void setFade()
        {
            const byte Opaco = byte.MaxValue;

            switch (state)
            {
                case ScreenState.Starting:
                    logo.Fade = (byte)(Opaco * MathHelper.SmoothStep(1f, 0f, (float)timer.TotalSeconds));
                    break;

                case ScreenState.Ending:
                    logo.Fade = (byte)(Opaco * MathHelper.SmoothStep(0f, 1f, (float)timer.TotalSeconds));
                    break;

                default: break;
            }
        }
    }
}